//
//  RHOtherFieldsCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/6/15.
//

#import "RHOtherFieldsCell.h"
@interface RHOtherFieldsCell()

@property (nonatomic ,strong)DeviceChartFieldModel *model;

@end
@implementation RHOtherFieldsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}
- (void)setModel:(DeviceChartFieldModel *)model deviceTypeCode:(NSString *)deviceTypeCode
{
    _model = model;
    if (_model) {
        [self setHidden:NO];
        [_titleLab setText:_model.keyName];
        NSLog(@"---->key = %@ , type = %@",model.keyCode,model.dataType);
        if (ValidStr(_model.content)) {
            
            NSString *content = [NSString stringWithFormat:@"%@%@",_model.content,ValidStr(_model.unitName)?_model.unitName:@""];
           
            if([model.dataType isEqualToString:@"float"]||[model.dataType isEqualToString:@"int"]){
                NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithString:model.content];
                NSString *valueStr = [NSString stringWithFormat:@"%@",value];
                content = [NSString stringWithFormat:@"%@%@",valueStr,ValidStr(_model.unitName)?_model.unitName:@""];
            }
            //master 和 pack 的剩余容量精度处理
//            if([model.keyCode isEqualToString:@"Mtr_RSOC"]||[model.keyCode isEqualToString:@"Pck_RSOC"]){
//                NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithString:model.content];
//                NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithString:model.accuracy];
//                NSDecimalNumber *result = [A decimalNumberByMultiplyingBy:B];
//                NSString *resultString = [NSString stringWithFormat:@"%@",result];
//                NSString *contentStr = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[resultString doubleValue]];
//                content = [NSString stringWithFormat:@"%@%@",contentStr,ValidNullData(model.unitName)];
//            }
            //枚举类型
            if (_model.isEnum) {
                NSInteger idx = [model.content integerValue];
               

                     //找出对应下标的枚举值
                     __block NSInteger index = -1;
                     [model.enumIndexs enumerateObjectsUsingBlock:^(NSString*  _Nonnull enumIndex, NSUInteger idx, BOOL * _Nonnull stop) {
                         if ([model.content integerValue] == enumIndex.integerValue) {
                             index = idx;
                         }
                     }];
                     //找到了就显示对应的枚举值
                     if (index != -1) {
                         content = model.enumValues[idx];
                     }else{
                         //没找到,显示后台返回的值
                         //content = ValidNullData(model.content);
                         content = @"--";
                     }

                
            }
            //master下的最高最低电压,温度位置
            if ([model.keyCode isEqualToString:@"Mtr_HV_LOC"]||
                [model.keyCode isEqualToString:@"Mtr_LV_LOC"]||
                [model.keyCode isEqualToString:@"Mtr_HT_LOC"]||
                [model.keyCode isEqualToString:@"Mtr_LT_LOC"]||
                [model.keyCode isEqualToString:@"Ems_HV_LOC"]||
                [model.keyCode isEqualToString:@"Ems_LV_LOC"]||
                [model.keyCode isEqualToString:@"Ems_HT_LOC"]||
                [model.keyCode isEqualToString:@"Ems_LT_LOC"]) {
                NSInteger value = [model.content integerValue];
                NSInteger h_loc = value >> 8;
                NSInteger l_loc = value & 0xFF;
                content = [NSString stringWithFormat:@"%ld_%ld",h_loc,l_loc];
            }
            
            //233 EMU参数信息中的最大可充放电功率
            if ([model.keyCode isEqualToString:@"Emu_MaxChgCur*Emu_TV"]||
                [model.keyCode isEqualToString:@"Emu_MaxDischgCur*Emu_TV"]) {
                NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithString:model.content];
                NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithDouble:1000];
                NSDecimalNumber *result = [A decimalNumberByDividingBy:B];
                NSString *resultString = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[result doubleValue]];
                content = [NSString stringWithFormat:@"%@%@",resultString,ValidStr(_model.unitName)?_model.unitName:@""];
            }

            //233   Mtr_DesignCap01(设计容量)   Mtr_FCC01(满充容量) Mtr_RSOC(剩余容量) 这两个字段要除1000
            if (([model.keyCode isEqualToString:@"Mtr_DesignCap01"]||
                [model.keyCode isEqualToString:@"Mtr_FCC01"]||[model.keyCode isEqualToString:@"Mtr_RSOC"])&&([deviceTypeCode is233DeviceType])) {
                NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithString:content];
                NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithString:@"1000"];
                NSDecimalNumber *result = [A decimalNumberByDividingBy:B];
                NSString *resultString = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[result doubleValue]];
                content = [NSString stringWithFormat:@"%@%@",resultString,ValidNullData(_model.unitName)];
            }
            
            //233液冷 最大温度,平均温度等为fault的情况
            if ([model.content isEqualToString:@"fault"]) {
                content = model.content;
            }
            
            [_contentLab setText:content];
        }else{
            [_contentLab setText:[NSString stringWithFormat:@"--%@",model.unitName]];
        }
    }else{
        [self setHidden:YES];
    }
    
    kWeakSelf;
    [self.contentLab sizeToFit];
    [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(weakSelf.contentLab.width+1);
    }];
}
- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,15, 0, 15));
       
    }];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    [_titleLab setFont:kSYSTEMFONT(14)];
    [_titleLab setTextColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6]];
    _titleLab.numberOfLines = 0;
    [bgView addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(12);
        make.bottom.mas_offset(-12);
    }];
    
    
    _contentLab = [[QMUILabel alloc] init];
    [_contentLab setTextAlignment:NSTextAlignmentRight];
    [_contentLab setFont:kSYSTEMFONT(14)];
    [_contentLab setTextColor:[UIColor colorWithHexString:@"3F4443" alpha:0.8]];
    [bgView addSubview:_contentLab];
    [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(weakSelf.titleLab);
        make.left.equalTo(weakSelf.titleLab.mas_right).offset(5);
    }];
    
    _line = [[UIView alloc]init];
    _line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
    [bgView addSubview:_line];
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.offset(1);
        make.bottom.mas_offset(0);
    }];
}

@end
